#include <stdio.h>

void quick_sort(int array[], int left, int right)
{
	int i = left, j = right;
	int temp;
	int pivot;

	pivot = array[(left + right) / 2];

	while (i <= j)
	{
		//从左到右找到大于等于基准点的元素
		while (array[i] < pivot)
		{
			i++;
		}
		//从右到左找到小于等于基准点的元素
		while (array[j] > pivot)
		{
			j--;
		}
		//如果 i<= j, 则互换元素
		if (i <= j)
		{
			temp = array[i];
			array[i] = array[j];
			array[j] = temp;
			i++;
			j--;
		}
		
	}

	if (left < j)
	{
		quick_sort(array, left, j);
	}
	
	if (i < right)
	{
		quick_sort(array, i, right);
	}
	
}

int main()
{
	int array[] = {22, 33, 101, 66, 67, 2, 13, 11, 77, 43};
	int i, length;
	
	length = sizeof(array) / sizeof(array[0]);
	quick_sort(array, 0, length-1);

	printf("排序后的结果是：\n");
	for (i = 0; i < length; i++)
	{
		printf("%d\n",array[i]);
	}	
	
	return 0;
}
